import easygui
import json
import sys,os
import requests
import time
from bs4 import BeautifulSoup


headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0 Safari/537.36"
}

# 读取JSON文件
filename = "information_book.json"
with open(filename, "r", encoding="utf-8") as f:
    information_book = json.load(f)

keys = []
# 遍历字典并打印键值对
for key, value in information_book.items():
    keys.append(key)

    
k = easygui.choicebox(msg='chose book', title='book reader', choices=keys, preselect=0, callback=None, run=True)
if k== None:
    sys.exit()
    
if not os.path.exists(str(keys.index(k)+1)+k):
    os.makedirs(str(keys.index(k)+1)+k)

list=information_book[k]
jq_num=1
for i in list:
    soup = BeautifulSoup(i, "html.parser").find('a')
    jqname = soup.get_text(strip=True)
    url="https://wiki.biligame.com/sr/index.php?title="+jqname+"&action=edit"
    time.sleep(1)
    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        soup = BeautifulSoup(response.text, "html.parser")
    
        # 找到textarea标签
        textarea = soup.find("textarea")
    
        if textarea:
            content = textarea.text
            file_path = os.path.join(str(keys.index(k)+1)+k, str(jq_num)+jqname + ".txt")
            jq_num+=1
            with open(file_path, 'w', encoding='utf-8') as file:
                file.write(content)
        else:
            print("页面中没有找到textarea标签")
    else:
        print(f"请求失败，状态码：{response.status_code}")
    